/**
  ******************************************************************************
  * @file    gpio.c
  * @brief   This file provides code for the configuration
  *          of all used GPIO pins.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */


#include "gpio.h"



extern uint8_t backup_pwr;
extern uint8_t MCU_IO_IN_Val;
extern uint8_t Rely1_Delay_flag;
extern uint8_t Rely2_Delay_flag;
extern uint8_t Rely3_Delay_flag;
extern uint8_t Rely4_Delay_flag;
extern uint32_t Rely1_Delay_Tick;
extern uint32_t Rely2_Delay_Tick;
extern uint32_t Rely3_Delay_Tick;
extern uint32_t Rely4_Delay_Tick;
extern uint8_t Rely1_status;
extern uint8_t Rely2_status;
extern uint8_t Rely3_status;
extern uint8_t Rely4_status;









/** Configure pins as
        * Analog
        * Input
        * Output
        * EVENT_OUT
        * EXTI
        * Free pins are configured automatically as Analog (this feature is enabled through
        * the Code Generation settings)
*/
void MX_GPIO_Init(void)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};

  
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();

  
  HAL_GPIO_WritePin(GPIOC, LED1_Pin|LED_NET_Pin, GPIO_PIN_RESET);

  
  HAL_GPIO_WritePin(GPIO_UART_TXD_GPIO_Port, GPIO_UART_TXD_Pin, GPIO_PIN_SET);

  
  HAL_GPIO_WritePin(GPIOH, HL10_AT_Pin|HL10_WAKEUP_Pin, GPIO_PIN_RESET);

  
  HAL_GPIO_WritePin(GPIOA, RS485_1_EN_Pin|LED2_Pin|LED3_Pin|CFG0_9120_Pin|MCU_REALY_EN_1_Pin|MCU_REALY_EN_2_Pin
                          |MCU_REALY_EN_3_Pin|MCU_REALY_EN_4_Pin, GPIO_PIN_RESET);

  
  HAL_GPIO_WritePin(GPIOB, EC20_PW_ON_Pin|EC20_RESET_Pin, GPIO_PIN_SET);

  
  HAL_GPIO_WritePin(GPIOB, LED4_Pin|LED5_Pin|LED6_Pin|RSTI_9120_Pin
                          |LED7_Pin|LED8_Pin, GPIO_PIN_RESET);

  
  GPIO_InitStruct.Pin = LED1_Pin|GPIO_UART_TXD_Pin|LED_NET_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  
  GPIO_InitStruct.Pin = HL10_AT_Pin|HL10_WAKEUP_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);

  
  GPIO_InitStruct.Pin = RS485_1_EN_Pin|LED2_Pin|LED3_Pin|CFG0_9120_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /*Configure GPIO pins : PBPin PBPin PBPin PBPin
                           PBPin PBPin PBPin PBPin
                           PBPin PBPin PBPin PBPin */
  GPIO_InitStruct.Pin = EC20_PW_ON_Pin|EC20_RESET_Pin|LED4_Pin|LED5_Pin
                          |LED6_Pin|RSTI_9120_Pin|MCU_REALY_EN_1_Pin|MCU_REALY_EN_2_Pin
                          |MCU_REALY_EN_3_Pin|MCU_REALY_EN_4_Pin|LED7_Pin|LED8_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  
  GPIO_InitStruct.Pin = GPIO_PIN_2;
  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  
  GPIO_InitStruct.Pin = MCU_IO_IN_1_Pin|MCU_IO_IN_2_Pin|MCU_IO_IN_3_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  
  GPIO_InitStruct.Pin = MCU_IO_IN_4_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(MCU_IO_IN_4_GPIO_Port, &GPIO_InitStruct);

}



/*
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
	if(GPIO_Pin == GPIO_PIN_5)
		backup_pwr = 1;
}
*/

void GetIo_Level(void)
{
	if(HAL_GPIO_ReadPin(MCU_IO_IN_1_GPIO_Port, MCU_IO_IN_1_Pin) == GPIO_PIN_RESET)
	{
		MCU_IO_IN_Val = MCU_IO_IN_Val | 0x01;
		LED5_OFF;
	}
	else 
	{
		MCU_IO_IN_Val = MCU_IO_IN_Val & 0x0e;
		LED5_ON;
	}
	
	if(HAL_GPIO_ReadPin(MCU_IO_IN_2_GPIO_Port, MCU_IO_IN_2_Pin) == GPIO_PIN_RESET)
	{
		MCU_IO_IN_Val = MCU_IO_IN_Val | 0x02;
		LED6_OFF;
	}
	else 
	{
		MCU_IO_IN_Val = MCU_IO_IN_Val & 0x0d;
		LED6_ON;
	}
	
	if(HAL_GPIO_ReadPin(MCU_IO_IN_3_GPIO_Port, MCU_IO_IN_3_Pin) == GPIO_PIN_RESET)
	{
		MCU_IO_IN_Val = MCU_IO_IN_Val | 0x04;
		LED7_OFF;
	}
	else 
	{
		MCU_IO_IN_Val = MCU_IO_IN_Val & 0x0b;
		LED7_ON;
	}
	
	if(HAL_GPIO_ReadPin(MCU_IO_IN_4_GPIO_Port, MCU_IO_IN_4_Pin) == GPIO_PIN_RESET)
	{
		MCU_IO_IN_Val = MCU_IO_IN_Val | 0x08;
		LED8_OFF;
	}
	else 
	{
		MCU_IO_IN_Val = MCU_IO_IN_Val & 0x07;
		LED8_ON;
	}
}

void Set_Led(uint8_t val)
{
	if( (val&0x01) ==0x01 ) 
		LED1_ON;
		
	if( (val&0x02) ==0x02 ) 
		LED2_ON;
		
	if( (val&0x04) ==0x04 ) 
		LED3_ON;
	
	if( (val&0x08) ==0x08 ) 
		LED4_ON;
	
	if( (val&0x10) ==0x10 ) 
		LED5_ON;
		
	if( (val&0x20) ==0x20 ) 
		LED6_ON;
		
	if( (val&0x40) ==0x40 ) 
		LED7_ON;
		
	if( (val&0x80) ==0x80 ) 
		LED8_ON;
}

void Reset_Led(uint8_t val)
{
	if( (val&0x01) ==0x01 ) 
		LED1_OFF;
		
	if( (val&0x02) ==0x02 ) 
		LED2_OFF;
		
	if( (val&0x04) ==0x04 ) 
		LED3_OFF;
	
	if( (val&0x08) ==0x08 ) 
		LED4_OFF;
	
	if( (val&0x10) ==0x10 ) 
		LED5_OFF;
		
	if( (val&0x20) ==0x20 ) 
		LED6_OFF;
		
	if( (val&0x40) ==0x40 ) 
		LED7_OFF;
		
	if( (val&0x80) ==0x80 ) 
		LED8_OFF;
}

void Set_Rely(uint8_t val)
{
	if((val>='0') && (val<='9'))
		val = val - '0';
	else if((val>='a') && (val<='f'))
		val = val - 0x57;
	else if((val>='A') && (val<='F'))
		val = val - 0x37;
	else
		;
	
	if( (val&0x01) ==0x01 ) 
	{
		RELY1_ON;
		Rely1_Delay_flag = 0;
		LED1_ON;
		Rely1_status = '1';
	}
		
	if( (val&0x02) ==0x02 ) 
	{
		RELY2_ON;
		Rely2_Delay_flag = 0;
		LED2_ON;
		Rely2_status = '1';
	}
		
	if( (val&0x04) ==0x04 ) 
	{
		RELY3_ON;
		Rely3_Delay_flag = 0;
		LED3_ON;
		Rely3_status = '1';
	}
		
	if( (val&0x08) ==0x08 ) 
	{
		RELY4_ON;
		Rely4_Delay_flag = 0;
		LED4_ON;
		Rely4_status = '1';
	}
}


void Reset_Rely(uint8_t val)
{
	if((val>='0') && (val<='9'))
		val = val - '0';
	else if((val>='a') && (val<='f'))
		val = val - 0x57;
	else if((val>='A') && (val<='F'))
		val = val - 0x37;
	else
		;
	
	if( (val&0x01) ==0x01 ) 
	{
		RELY1_OFF;
		Rely1_Delay_flag = 0;
		LED1_OFF;
		Rely1_status = '0';
	}
		
	if( (val&0x02) ==0x02 ) 
	{
		RELY2_OFF;
		Rely2_Delay_flag = 0;
		LED2_OFF;
		Rely2_status = '0';
	}
		
	if( (val&0x04) ==0x04 ) 
	{
		RELY3_OFF;
		Rely3_Delay_flag = 0;
		LED3_OFF;
		Rely3_status = '0';
	}
	
	if( (val&0x08) ==0x08 ) 
	{
		RELY4_OFF;
		Rely4_Delay_flag = 0;
		LED4_OFF;
		Rely4_status = '0';
	}

}


void Delay_Rely(uint8_t val)
{
	if((val>='0') && (val<='9'))
		val = val - '0';
	else if((val>='a') && (val<='f'))
		val = val - 0x57;
	else if((val>='A') && (val<='F'))
		val = val - 0x37;
	else
		;
	
	if( (val&0x01) ==0x01 ) 
	{
		RELY1_ON;
		Rely1_Delay_flag = 1;
		Rely1_Delay_Tick = 0;
		LED1_ON;
		Rely1_status = '1';
	}
		
	if( (val&0x02) ==0x02 ) 
	{
		RELY2_ON;
		Rely2_Delay_flag = 1;
		Rely2_Delay_Tick = 0;
		LED2_ON;
		Rely2_status = '1';
	}
		
	if( (val&0x04) ==0x04 ) 
	{
		RELY3_ON;
		Rely3_Delay_flag = 1;
		Rely3_Delay_Tick = 0;
		LED3_ON;
		Rely3_status = '1';
	}
		
	if( (val&0x08) ==0x08 ) 
	{
		RELY4_ON;
		Rely4_Delay_flag = 1;
		Rely4_Delay_Tick = 0;
		LED4_ON;
		Rely4_status = '1';
	}

}


void Poll_LED(uint8_t val)
{
	uint8_t i;
	i = val;
	while(i--)
	{
		LED1_ON;
		LED2_ON;
		LED3_ON;
		LED4_ON;
		LED5_ON;
		LED6_ON;
		LED7_ON;
		LED8_ON;
		LEDNET_ON;
	
		HAL_Delay(150);
	
		LED1_OFF;
		LED2_OFF;
		LED3_OFF;
		LED4_OFF;
		LED5_OFF;
		LED6_OFF;
		LED7_OFF;
		LED8_OFF;
		LEDNET_OFF;
	
		HAL_Delay(150);
	}
}






